using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Diagnostics.Contracts;
using Konst3d.Efficiency.Suite;

namespace PEM
{
	internal partial class MonitoringStatisticViewModel
	{
		#region Commands

		public abstract class BaseCommand : ICommand
		{
			#region Constructor

			public BaseCommand(MonitoringStatisticViewModel model)
			{
				Contract.Requires(model != null);
				_model = model;
			}

			#endregion // Constructor

			#region ICommand Implementation

			public event System.EventHandler CanExecuteChanged;

			public abstract bool CanExecute(object parameter);
			public abstract void Execute(object parameter);

			protected void NotifyCanExecuteChanged(EventArgs args = null)
			{
				if (CanExecuteChanged != null)
				{
					CanExecuteChanged(this, args ?? EventArgs.Empty);
				}
			}

			#endregion

			#region Fields

			protected MonitoringStatisticViewModel _model;

			#endregion // Fields
		}

		public class EditStatisticDayCommand : BaseCommand
		{
			#region Constructor

			public EditStatisticDayCommand(MonitoringStatisticViewModel host):
				base(host)
			{
				_model.DaySelected += new EventHandler(OnDaySelected);
			}

			#endregion // Constructor

			#region ICommand Members

			public override bool CanExecute(object parameter)
			{
				return _model.Selected != null &&
					_model.Selected.Date.Date >= _model.Monitor.Created.Date &&
					_model.Selected.Date.Date <= DateTime.Now.Date;
			}

			public override void Execute(object parameter)
			{
				_model.EditSelectedDay();
			}

			#endregion // ICommand Members

			#region Event Handlers

			private void OnDaySelected(MonitoringStatisticViewModel sender)
			{
				NotifyCanExecuteChanged();
			}

			#endregion // Event Handlers
		}

		#endregion // Commands

		#region Commands Collection

		public class CommandsCollection
		{
			#region Properties

			public EditStatisticDayCommand EditSelectedDay
			{
				get;
				private set;
			}

			#endregion // Properties

			#region Constructor

			public CommandsCollection(MonitoringStatisticViewModel host)
			{
				Contract.Requires(host != null);

				EditSelectedDay = new EditStatisticDayCommand(host);
			}

			#endregion // Constructor
		}

		#endregion // Commands Collection

		public void EditSelectedDay()
		{
			Contract.Requires(Selected != null);

			if (_editDayDlg.ShowDialog(_parentWindow, Selected))
			{
				Selected.Value = _editDayDlg.StatisticValue;
				Selected.Commentary = _editDayDlg.StatisticCommentary;

				_statistic.NotifyItemChanged(Selected);
			}
		}
	}
}
